import storage from './storage';
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('-')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

/**
 * @description 10位时间戳转换
 * @param time
 * @returns {string}
 */
function tenBitTimestamp(time, type = 'all') {
  const date = new Date(time * 1000)
  const y = date.getFullYear()
  let m = date.getMonth() + 1
  m = m < 10 ? '0' + m : m
  let d = date.getDate()
  d = d < 10 ? '0' + d : d
  let h = date.getHours()
  h = h < 10 ? '0' + h : h
  let minute = date.getMinutes()
  let second = date.getSeconds()
  minute = minute < 10 ? '0' + minute : minute
  second = second < 10 ? '0' + second : second
  if (type == 'all') {
    return `${y}-${m}-${d} ${h}:${minute}:${second}`
  } else {
    return `${y}-${m}-${d}`
  }

}

function showLoading(text = '加载中...', delay = 0) {
  return wx.showLoading({
    title: text,
    mask: true,
    delay: delay
  })
}
function hideLoading() {
  return wx.hideLoading();
}

/**
 * @description toast
 * @param {string} text 
 */
const toast = (text) => {
  return wx.showToast({
    title: text,
    icon: 'none',
		duration: 1500
  })
}
export function hasLogin() {
  if (storage.get('token')) {
    return true
  } else {
    return false
  }
}
/**
 * 
 * @param {string} nonceStr nonceStr
 * @param {string} packages packages
 * @param {string} paySign paySign
 * @param {string} signType signType
 * @param {string} timeStamp timeStamp
 * @param {string} callback callback
 */
export function wxPayment(res, callback, fail) {
  wx.requestPayment({
    ...res,
    success() {
      callback && callback()
    },
    fail(err) {
      toast('取消支付')
      fail && fail();
    }
  })
}

function splitText(ctx, text, font, maxTextWidth, maxRowLength) {
  let chr = text.split('');
  let temp = '';
  let row = [];
  ctx.fillStyle = '#000'
  ctx.font = font
  for (let a = 0; a < chr.length; a++) {
    if (ctx.measureText(temp).width < maxTextWidth) {
      temp += chr[a];
    } else {
      a--;
      row.push(temp);
      temp = '';
    }
  }
  row.push(temp);
  if (row.length > maxRowLength) {
    let rowCut = row.slice(0, maxRowLength);
    let rowPart = rowCut[maxRowLength - 1];
    let test = '';
    let empty = [];
    for (var a = 0; a < rowPart.length; a++) {
      if (ctx.measureText(test).width < maxTextWidth) {
        test += rowPart[a];
      } else {
        break;
      }
    }
    empty.push(test);
    let group = empty[0] + '...';
    rowCut.splice(maxRowLength - 1, 1, group);
    row = rowCut;
  }
  return row
}
/**
 * @description 将url请求参数转为json格式
 * @param url
 * @returns {{}|any}
 */
function paramObj(url) {
  const search = url.split('?')[1]
  if (!search) {
    return {}
  }
  return JSON.parse(
    '{"' +
    decodeURIComponent(search)
      .replace(/"/g, '\\"')
      .replace(/&/g, '","')
      .replace(/=/g, '":"')
      .replace(/\+/g, ' ') +
    '"}'
  )
}

module.exports = {
  tenBitTimestamp,
  formatTime,
  showLoading,
  hideLoading,
  toast,
  hasLogin,
  wxPayment,
  splitText,
  paramObj
}
